package com.ming.springcloud.consumer.controller;

import com.alibaba.fastjson.JSONObject;
import com.ming.springcloud.consumer.service.BookService;
import com.ming.springcloud.consumer.util.ResponseEntity;
import com.ming.springcloud.consumer.vo.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/book_hystric")
public class BookHystricController {
    @Autowired
    private BookService bookService;


    @RequestMapping(value = "findAllBooks", method = RequestMethod.GET)
    public ResponseEntity findAllStudent1(){
        String userJSON= bookService.findAllBooks("ming");
        return  ResponseEntity.success().add("pageInfo",userJSON);
    }

    @RequestMapping(value = "findBooksById/{id}", method = RequestMethod.GET)
    public ResponseEntity findStudentsById(@PathVariable("id")Integer id){
        String userJSON= bookService.findBooksById("ming",id);
        JSONObject object = (JSONObject) JSONObject.parse(userJSON);

        //获取回传的josn数据
        String bookId=object.get("bookId").toString();
        String bookName=object.get("bookName").toString();
        String publishing=object.get("publishing").toString();
        String serviceName=object.get("serviceName").toString();
        String author=object.get("author").toString();
        String recommend=object.get("recommend").toString();
        String price=object.get("price").toString();
        String ids=object.get("id").toString();

        Books book=new Books();
        book.setId(Integer.parseInt(ids));
        book.setServiceName(serviceName);
        book.setAuthor(author);
        book.setBookId(bookId);
        book.setBookName(bookName);
        book.setPrice(Integer.parseInt(price));
        book.setPublishing(publishing);
        book.setRecommend(recommend);

        return  ResponseEntity.success().add("user",book);
    }

    @RequestMapping(value = "delBook/{id}", method = RequestMethod.GET)
    public ResponseEntity delStudent(@PathVariable("id")Integer id){
        String userJSON= bookService.delBook("ming",id);
        JSONObject object = (JSONObject) JSONObject.parse(userJSON);
        Integer result =Integer.parseInt(object.get("code").toString());
        if(result>0){
            return ResponseEntity.success();
        }else{
            return ResponseEntity.error("删除失败");
        }
    }

    @PostMapping("/editBook")
    public ResponseEntity addOrEditUser(Books book){
        if(book==null){
            return ResponseEntity.error("参数错误");
        }
        if(book.getId()==null){//新增
            String userJSON=bookService.addBook("ming",book);

            JSONObject object = (JSONObject) JSONObject.parse(userJSON);
            Integer result =Integer.parseInt(object.get("code").toString());

            if(result>0){
                return ResponseEntity.success();
            }else{
                return ResponseEntity.error("新增失败");
            }
        }else{
            String userJSON=bookService.editBook("ming",book);

            JSONObject object = (JSONObject) JSONObject.parse(userJSON);
            Integer result =Integer.parseInt(object.get("code").toString());

            if(result>0){
                return ResponseEntity.success();
            }else{
                return ResponseEntity.error("修改失败");
            }
        }
    }
}
